using System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Data;
using System.Web.Configuration;
using System.Web.UI.WebControls;
using System.Web.Security;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace Starznet.SharePoint.SPDAT.ApplicationPages.Settings
{
    public class MembershipUserListPage : LayoutsPageBase
    {
        protected SPGridView GvFormsAuthUserList;
        private MenuTemplate _oUserListMenu;
        private DataView _oView;
        protected ToolBarButton idAddField;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            GvFormsAuthUserList.Sorting += new GridViewSortEventHandler(GvFormsAuthUserList_Sorting);
            GvFormsAuthUserList.Columns.Clear();

            _oUserListMenu = new MenuTemplate();
            _oUserListMenu.ID = "UserListMenu";

            SPMenuField oUserMenu = new SPMenuField();
            oUserMenu.HeaderText = this.GetGlobalResourceObject("szadm", "UserList_UsernameColumn").ToString();
            oUserMenu.TextFields = "UserName";
            oUserMenu.MenuTemplateId = "UserListMenu";
            oUserMenu.NavigateUrlFields = "UserName";
            oUserMenu.NavigateUrlFormat = "MembershipEditUser.aspx?UserName={0}";
            oUserMenu.TokenNameAndValueFields = "NAME=UserName";
            oUserMenu.SortExpression = "UserName";

            //MenuItemTemplate oEditUser = new MenuItemTemplate("Edit user", "/_layouts/images/EDITITEM.GIF");
            //oEditUser.ClientOnClickNavigateUrl = this.Context.Request.RawUrl + "?UserAdminMode=Edit" +
            //                                        "&UserName=%NAME%";
            //_oUserListMenu.Controls.Add(oEditUser);

            MenuItemTemplate oDeleteUser = new MenuItemTemplate(this.GetGlobalResourceObject("szadm", "UserList_DeleteUserMenu").ToString(), "/_layouts/images/DELITEM.GIF");
            oDeleteUser.ClientOnClickNavigateUrl = "MembershipDeleteUser.aspx?UserName=%NAME%";
            _oUserListMenu.Controls.Add(oDeleteUser);

            //Only show reset password if membership provider allows it
            if (Membership.EnablePasswordReset)
            {
                MenuItemTemplate oResetPass = new MenuItemTemplate(this.GetGlobalResourceObject("szadm", "UserList_ResetPassMenu").ToString(), "/_layouts/images/manageperm.gif");
                oResetPass.ClientOnClickNavigateUrl = "MembershipResetPass.aspx?UserName=%NAME%";
                 _oUserListMenu.Controls.Add(oResetPass);
            }

            this.Page.Controls.Add(_oUserListMenu);
            GvFormsAuthUserList.Columns.Add(oUserMenu);

            BoundField oEmailField = new BoundField();
            oEmailField.DataField = "Email";
            oEmailField.HeaderText = this.GetGlobalResourceObject("szadm", "UserList_EmailColumn").ToString();
            oEmailField.SortExpression = "Email";
            GvFormsAuthUserList.Columns.Add(oEmailField);

            BoundField oLastLogin = new BoundField();
            oLastLogin.DataField = "LastLogin";
            oLastLogin.HeaderText = this.GetGlobalResourceObject("szadm", "UserList_LastLoginColumn").ToString();
            oLastLogin.SortExpression = "LastLogin";
            GvFormsAuthUserList.Columns.Add(oLastLogin);

            this.PopulateUserListData();
        }

        protected void GvFormsAuthUserList_Sorting(object sender, GridViewSortEventArgs e)
        {
            String sLastExpression = "";
            if (ViewState["SortExpression"] != null)
                sLastExpression = ViewState["SortExpression"].ToString();

            String sLastDirection = "asc";
            if (ViewState["SortDirection"] != null)
                sLastDirection = ViewState["SortDirection"].ToString();

            String sNewDirection = "asc";
            if (e.SortExpression == sLastExpression)
                sNewDirection = (sLastDirection == "asc") ? "desc" : "asc";

            ViewState["SortExpression"] = e.SortExpression;
            ViewState["SortDirection"] = sNewDirection;

            _oView.Sort = e.SortExpression + " " + sNewDirection;
            GvFormsAuthUserList.DataBind();
        }

        private void PopulateUserListData()
        {
            DataTable oDT = new DataTable();
            oDT.Columns.Add("UserName");
            oDT.Columns.Add("Email");
            oDT.Columns.Add("LastLogin");
            oDT.Columns.Add("WebSiteName");
            oDT.Columns.Add("Url");

            MembershipUserCollection oUsers = Membership.GetAllUsers();
            foreach (MembershipUser oUser in oUsers)
            {
                DataRow oRow = oDT.NewRow();
                oRow["UserName"] = oUser.UserName;
                oRow["Email"] = oUser.Email;
                oRow["LastLogin"] = oUser.LastLoginDate.ToString();

                oDT.Rows.Add(oRow);
            }
            _oView = new DataView(oDT);
            GvFormsAuthUserList.DataSource = _oView;
            GvFormsAuthUserList.DataBind();
        }
    }
}